package com.aizhixin.lab.course.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.PageData;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.SignInDomain;
import com.aizhixin.lab.config.Config;
import com.aizhixin.lab.course.course.domain.*;
import com.aizhixin.lab.course.course.dto.CourseDTO;
import com.aizhixin.lab.course.course.entity.Course;
import com.aizhixin.lab.course.course.entity.CourseChapter;
import com.aizhixin.lab.course.course.service.*;
import com.aizhixin.lab.course.template.domain.TemplateDomain;
import com.aizhixin.lab.course.template.entity.Template;
import com.aizhixin.lab.course.template.service.TemplateService;

import com.aizhixin.lab.project.course.vo.GroupInfoVo;
import io.swagger.annotations.*;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.*;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/course/teacher")
@Api(value = "教师课程相关API")
public class TeacherCourseController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private CourseTemplateService courseTemplateService;
    @Autowired
    private TeacherCourseService teacherCourseService;
    @Autowired
    private TemplateService templateService;
    @Autowired
    private CoursePublishService coursePublishService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private ReleaseSignInService signInService;
    @Autowired
    private Config config;
    @RequestMapping(value = "/getTemplateList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程模板列表", response = Void.class, notes = "查询课程模板列表<br>@author hsh")
    public ResponseEntity<?> getTemplateList(@RequestHeader("Authorization") String token,
                                             @ApiParam(value = "name") @RequestParam(value = "name", required = false) String name,
                                             @ApiParam(value = "type 0:全部 10:理论课 20:实验课") @RequestParam(value = "type", required = false) Integer type,
                                             @ApiParam(value = "专业ID") @RequestParam(value = "profId", required = false) Long profId,
                                             @ApiParam(value = "pageNumber 起始页") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
                                             @ApiParam(value = "pageSize 每页的限制数目") @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                             @ApiParam(value = "是否过滤掉知识点为0的课程") @RequestParam(value = "section", required = false) Integer section) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        PageData<TemplateDomain> result = templateService.getPublishedList(name, type,profId, pageNumber, pageSize,account,section);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/createCourse", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "创建课程", response = Void.class, notes = "创建课程<br>@author hsh")
    public ResponseEntity<?> createCourse(@RequestHeader("Authorization") String token,
                                          @ApiParam(value = "templateId", required = true) @RequestParam(value = "templateId") Long templateId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = courseTemplateService.createCourse(token, account.getId(), templateId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程列表", response = Void.class, notes = "查询课程列表<br>@author hsh")
    public ResponseEntity<?> getList(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "name", required = false) @RequestParam(value = "name", required = false) String name,
                                     @ApiParam(value = "type 0:全部 10:理论课 20:实验课", required = false) @RequestParam(value = "type", required = false) Integer type,
                                     @ApiParam(value = "pageNumber 起始页") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
                                     @ApiParam(value = "pageSize 每页的限制数目") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        PageData<CourseBaseDomain> result = teacherCourseService.getPublishedList(account.getId(), false, name, type, false, pageNumber, pageSize, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getPublishedList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询已发布课程列表", response = Void.class, notes = "查询已发布课程列表<br>@author hsh")
    public ResponseEntity<?> getPublishedList(@RequestHeader("Authorization") String token,
                                              @ApiParam(value = "name", required = false) @RequestParam(value = "name", required = false) String name,
                                              @ApiParam(value = "isArchive", required = true) @RequestParam(value = "isArchive", required = true) Boolean isArchive,
                                              @ApiParam(value = "type 0:全部 10:理论课 20:实验课", required = false) @RequestParam(value = "type", required = false) Integer type,
                                              @ApiParam(value = "pageNumber 起始页") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
                                              @ApiParam(value = "pageSize 每页的限制数目") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        PageData<CourseBaseDomain> result = teacherCourseService.getPublishedList(account.getId(), isArchive, name, type, true, pageNumber, pageSize, false);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getRecentlyList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询最近授课课程列表,2条", response = Void.class, notes = "查询最近授课课程列表,2条<br>@author hsh")
    public ResponseEntity<?> getRecentlyList(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<CourseBaseDomain> result = teacherCourseService.getRecentlyList(account.getId());
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getMyCourseList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询我的所有课程列表", response = Void.class, notes = "查询我的所有课程列表<br>@author lwq")
    public ResponseEntity<?> getMyRecentlyList(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<Course> result = courseService.findMyCourse(account.getId());

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/getMyCourseListNew", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "New查询我的所有课程列表", response = Void.class, notes = "New查询我的所有课程列表<br>@author lwq")
    public ResponseEntity<?> getMyRecentlyListNew(@RequestHeader("Authorization") String token) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<Course> result = courseService.findByCreatedByAndDeleteFlag(account.getId());
        return new ResponseEntity(result, HttpStatus.OK);
    }




    @RequestMapping(value = "/recently", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "授课", response = Void.class, notes = "授课<br>@author hsh")
    public ResponseEntity<?> recently(@RequestHeader("Authorization") String token,
                                      @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = teacherCourseService.recently(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getCourse", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程,编辑时用", response = Void.class, notes = "查询课程,编辑时用<br>@author hsh")
    public ResponseEntity<?> getCourse(@RequestHeader("Authorization") String token,
                                       @ApiParam(value = "courseId", required = false) @RequestParam(value = "courseId", required = false) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        CourseDomain result = teacherCourseService.getCourse(account, courseId, true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getCourseWithoutHide", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程,预览时用", response = Void.class, notes = "查询课程,预览时用<br>@author hsh")
    public ResponseEntity<?> getCourseWithoutHide(@RequestHeader("Authorization") String token,
                                                  @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        CourseDomain result = teacherCourseService.getCourse(account, courseId, false);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/publish", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "发布课程", response = Void.class, notes = "发布课程<br>@author hsh")
    public ResponseEntity<?> publish(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = coursePublishService.publish(token, account.getId(), courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/archive", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "课程归档", response = Void.class, notes = "课程归档<br>@author hsh")
    public ResponseEntity<?> archive(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = teacherCourseService.archive(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "保存课程", response = Void.class, notes = "保存课程<br>@author hsh")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token,
                                  @ApiParam(value = "type 10:理论课 20:实验课", required = false) @RequestBody CourseDTO dto) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = teacherCourseService.save(dto,true);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除课程", response = Void.class, notes = "删除课程<br>@author hsh")
    public ResponseEntity<?> delete(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = teacherCourseService.delete(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/deleteList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "批量删除课程", response = Void.class, notes = "批量删除课程<br>@author hsh")
    public ResponseEntity<?> deleteList(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "courseId", required = true) @RequestBody DeleteDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result=null;
        for (String s:domain.getCourseIds()){
            result   = teacherCourseService.delete(s);
            if (!(Boolean) result.get(ApiReturnConstants.SUCCESS)){
                break;
            }
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getTrashList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询已删除课程列表", response = Void.class, notes = "查询已删除课程列表<br>@author hsh")
    public ResponseEntity<?> getTrashList(@RequestHeader("Authorization") String token,
                                          @ApiParam(value = "name", required = false) @RequestParam(value = "name", required = false) String name,
                                          @ApiParam(value = "pageNumber 起始页") @RequestParam(value = "pageNumber", required = false) Integer pageNumber,
                                          @ApiParam(value = "pageSize 每页的限制数目") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        PageData<CourseBaseDomain> result = teacherCourseService.getTrashList(account.getId(), name, pageNumber, pageSize);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/putBack", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "恢复课程", response = Void.class, notes = "恢复课程<br>@author hsh")
    public ResponseEntity<?> putBack(@RequestHeader("Authorization") String token,
                                     @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = teacherCourseService.putBack(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/emptyTrash", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "DELETE", value = "删除垃圾课程", response = Void.class, notes = "删除垃圾课程<br>@author hsh")
    public ResponseEntity<?> emptyTrash(@RequestHeader("Authorization") String token,
                                        @ApiParam(value = "courseId", required = true) @RequestParam(value = "courseId", required = true) String courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = teacherCourseService.emptyTrash(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }


    @RequestMapping(value = "/isused", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "课程中心课程是否被引用过(返回结果isUsed:true使用过,false未使用)", response = Void.class, notes = "课程中心课程是否被引用过<br>@author zhengning")
    public ResponseEntity<?> isUsed(@RequestHeader("Authorization") String token,
                                    @ApiParam(value = "courseId课程id", required = true) @RequestParam(value = "courseId", required = true) Long courseId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = new HashMap<>();
        Long countNum = courseService.countByTemplateId(courseId,account.getId());
        boolean isUsed = false;
        if (null != countNum && countNum > 0L) {
            isUsed = true;
        }
        result.put("isUsed", isUsed);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/getMyCourseChapterList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程列表下的章节列表", response = Void.class, notes = "查询课程列表下的章节列表<br>@author lwq")
    public ResponseEntity<?> getMyCourseChapterList(@RequestHeader("Authorization") String token,
                                                    @ApiParam(value = "courseId", required = false) @RequestParam(value = "courseId", required = false) String courseId
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<CourseChapter> result = coursePublishService.findChapterList(courseId);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/getMyChapterMinutiaList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "查询课程章节的小节名称列表", response = Void.class, notes = "查询课程章节的小节名称列表<br>@author lwq")
    public ResponseEntity<?> getMyChapterMinutiaList(@RequestHeader("Authorization") String token,
                                                    @ApiParam(value = "courseChapterId", required = false) @RequestParam(value = "courseChapterId", required = false) Long courseChapterId
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<CourseChapter> result = coursePublishService.findMinutiaList(courseChapterId);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/getCourseName", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "获取镜像关联的课程名称", response = Void.class, notes = "获取镜像关联的课程名称<br>@author lwq")
    public ResponseEntity<?> getCourseName(@RequestHeader("Authorization") String token,
                                                     @ApiParam(value = "imageName", required = false) @RequestParam(value = "imageName", required = false) String imageName
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(imageName)){
            return new ResponseEntity<Object>("imageName is null", HttpStatus.UNAUTHORIZED);
        }
        List<String> courseName = courseService.findCourseName(imageName);
        return new ResponseEntity(courseName, HttpStatus.OK);
    }

    @RequestMapping(value = "/findSignTable", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "获取课程下的签到表", response = Void.class, notes = "获取课程下的签到表<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ReleaseSignInData.class)})
    public ResponseEntity<?> findSignTable(@RequestHeader("Authorization") String token,
                                           @ApiParam(value = "courseId", required = false) @RequestParam(value = "courseId", required = false) String courseId,
                                           @RequestParam String date
                                           ) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> signTable = signInService.findSignTable(courseId, date);
        return new ResponseEntity(signTable, HttpStatus.OK);
    }
    @RequestMapping(value = "/signTableInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "POST", value = "查看签到表签到信息", response = Void.class, notes = "查看签到表签到信息<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuSignInData.class)})
    public ResponseEntity<?> signTableInfo(@RequestHeader("Authorization") String token,
                                           @RequestBody SignInDomain domain
    ) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> signTableInfo = signInService.findSignTableInfo(domain);
        return new ResponseEntity(signTableInfo, HttpStatus.OK);
    }

    @RequestMapping(value = "/exportSignInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "导出签到信息", notes = "导出签到信息<br>@author lwq")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = StuSignInData.class)})
    public ResponseEntity<?> exportSignInfo(@RequestHeader("Authorization") String token,
                                            @ApiParam(value = "courseId", required = false) @RequestParam(value = "courseId", required = false) String courseId,
                                            @RequestParam String date, HttpServletResponse response) throws IOException {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        List<ReleaseSignInData> signTable = signInService.getSignTable(courseId, date);
        HSSFWorkbook wb = new HSSFWorkbook();
        for (ReleaseSignInData r:signTable){
            HSSFSheet sheet = wb.createSheet("第"+r.getSignNum()+"次签到");
            HSSFRow row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("学号");
            row.createCell(2).setCellValue("姓名");
            row.createCell(3).setCellValue("性别");
            row.createCell(4).setCellValue("院系");
            row.createCell(5).setCellValue("专业");
            row.createCell(6).setCellValue("班级");
            row.createCell(7).setCellValue("签到状态");
            List<StuSignInData> signTableInfo = signInService.findSignTableInfo(r.getId());
            int i=1;
            for (StuSignInData stuSignInData : signTableInfo) {
                HSSFRow row1 = sheet.createRow(0);
                row1.createCell(0).setCellValue(i);
                row1.createCell(1).setCellValue(stuSignInData.getStuId());
                row1.createCell(2).setCellValue(stuSignInData.getStuName());
                row1.createCell(3).setCellValue(stuSignInData.getSex());
                row1.createCell(4).setCellValue(stuSignInData.getCollegeName());
                row1.createCell(5).setCellValue(stuSignInData.getProName());
                row1.createCell(6).setCellValue(stuSignInData.getClassName());
                row1.createCell(7).setCellValue(stuSignInData.getStatus());
                i++;
            }
        }
        FileOutputStream output = new FileOutputStream(config.getBaseDir() + "/aa.xls");
        wb.write(output);
        output.flush();
        String basePath = config.getBaseDir() + "/aa.xls";
        File file = new File(basePath);
        file.setWritable (false);
        // 取得文件名。
        String filename = file.getName().toString();

        // 取得文件的后缀名。
        String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

        // 以流的形式下载文件。
        InputStream fis = new BufferedInputStream(new FileInputStream(basePath));
        byte[] buffer = new byte[fis.available()];
        fis.read(buffer);
        fis.close();
        // 清空response
        response.reset();
        // 设置response的Header
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(System.currentTimeMillis() + ".xls", "UTF-8"));
        response.addHeader("Content-Length", "" + file.length());
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        toClient.write(buffer);
        toClient.flush();
        toClient.close();
        return new ResponseEntity(null, HttpStatus.OK);
    }

}
